const jwt = require('jsonwebtoken')

module.exports = (req, res, next) => {
    let {authorization} = req.headers
    if(!authorization) {
        res.json({
            error_code: 4,
            msg: '未检测到令牌！'
        })
        return
    }
    jwt.verify(authorization, '2210diancan', function(err, decoded) {
        if(err) {
            res.json({
                error_code: 5,
                msg: '令牌校验失败！'
            })
            return
        }
        let {startTime, expires} = decoded
        // console.log(startTime, expires, +new Date);
        if(startTime + expires < +new Date) {
            res.json({
                error_code: 6,
                msg: '令牌已经过期！'
            })
            return
        }
        next()
    });
}